/**
 * @author Ahammer
 * @date   2009 01
 * @update 
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package op;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.media.jai.ParameterBlockJAI;
import javax.media.jai.PlanarImage;
import javax.swing.ToolTipManager;
import opgui.IqmOperatorJFrame;
import main.Board;
import main.IqmTools;
import main.Look;
import main.ManagerFrame;
import main.Tank;

/**
 * This class provides static methods to control the current image processing function
 * 
 */
public class IqmOperator {

	/**
	 * This class initializes the current image processing function
	 * @param arg
	 */
	public static void initIqmOperator(String currFunc){
		
		//these functions don't need a valid image
		if(currFunc == "IqmFracIFS" || currFunc == "IqmCreateFracSurf" || currFunc == "IqmCreateImage"){
			//don't check for a valid image
		}
		else { //all other functions need at least one valid image
			if (IqmTools.getCurrManagerImgIndex() < 0){
				Board.appendTexln("IqmOperator:  No valid image index");
				return;
			}
			if (!(Tank.getCurrentTankImageAt(IqmTools.getCurrManagerImgIndex()) instanceof PlanarImage)){
				Board.appendTexln("IqmOperator:  No valid image available");
				return;
			}
		}
		

		if (currFunc == null){
			Board.appendTexln("IqmOperator:  Current function = null ");
			return;
		}
		//closeIqmOperator(arg); //falls schon aktiv
		if (currFunc.equals(IqmTools.getCurrImgProcessFunc())){
			Board.appendTexln("IqmOperator: "+currFunc+" already open");
			return;
		}
		if (IqmTools.getCurrImgProcessFunc()!= null){
			Board.appendTexln("IqmOperator: "+IqmTools.getCurrImgProcessFunc()+" already open");
			return;
		}
		ManagerFrame.setApplyButtonsEnabled(true);
	    ParameterBlockJAI pb = new ParameterBlockJAI(currFunc);
		IqmTools.setCurrParameterBlockJAI(pb);
	    IqmTools.setCurrImgProcessFunc(currFunc);
		//create and show proper GUI	
		Class cl  = null;
		Object ob = null;
		Method me = null;
		
		try {
			//MyClass = Class.forName("opgui."+arg+"GUI");  //eventuell
			cl = Class.forName("opgui."+currFunc+"GUI", true, Thread.currentThread().getContextClassLoader());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			//MyClass = Class.forName("opgui."+arg+"GUI"); //eventuell
		}
		try {
			ob = cl.getConstructor().newInstance();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		IqmTools.setCurrIqmOperatorGUI((IqmOperatorJFrame) ob);
		try {
			me = cl.getMethod("createIqmOperatorGUI");
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		try {
			me.invoke(ob);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} 
		
	}
	
	/**
	 * This method closes the current IqmOperator
	 * it destroys the GUI
	 */
	public static void closeCurrIqmOperator(){

        ToolTipManager ttm = ToolTipManager.sharedInstance();
        ttm.setInitialDelay(IqmTools.getDefaultToolTipInitDelay());  //ms
        ttm.setReshowDelay (IqmTools.getDefaultToolTipReshowDelay());
        ttm.setDismissDelay(IqmTools.getDefaultToolTipDismissDelay());
        
		//win.closeAndDestroyGUI();
		IqmOperatorJFrame gui = IqmTools.getCurrIqmOperatorGUI();
		//IqmOperatorJFrame win = IqmTools.getCurrImgProcessFuncWindow();
		gui.destroyIqmOperatorGUI();
		//win.setVisible(arg0);
		gui = null;
		
	    IqmTools.setCurrImgProcessFunc(null);
	    IqmTools.setCurrIqmOperatorGUI(null);
	    //IqmTools.setCurrIqmOperatorJFrame(null);
	   	IqmTools.setCurrParameterBlockJAI(null);
		PlanarImage pi = Tank.getCurrentTankImageAt(IqmTools.getCurrManagerImgIndex());
		if (pi != null) Look.setNewImage(pi);
		ManagerFrame.setApplyButtonsEnabled(false);
	}
	/**
	 * This method updates the current IqmOperator
	 * it destroys the GUI too
	 */
	public static void updateCurrIqmOperator(){
		IqmOperatorJFrame gui = IqmTools.getCurrIqmOperatorGUI();
		if (gui != null )gui.updateIqmOperatorGUI();	
	}

}//END
